package com.hanahina.project514.calc;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {

	JFrame jFrame;
	double x;
	String op="";
	
	public static void main(String[] args) {
		new Main().initView();
	}

	public void initView() {
		jFrame=new JFrame("计算器");
		jFrame.setBounds(490, 155, 300, 560);
		jFrame.setLayout(null);
		
		JTextField jTextField=new JTextField("0");						//添加文本框
		jTextField.setBounds(15, 20, 260, 50);
		jTextField.setHorizontalAlignment(JTextField.RIGHT);			//设置文本右对齐
		jTextField.setFont(new Font("微软雅黑", Font.BOLD, 30));			//设置字体、字型、字号
		jFrame.add(jTextField);
		
		JButton jButton0=new JButton("0");								//添加按钮0
		jButton0.setBounds(15, 370, 120, 50);
		jButton0.setHorizontalAlignment(JButton.CENTER);
		jButton0.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton0.addActionListener(new ActionListener() {				//为按钮添加事件监听器
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();	//
				if (!string.equals("0")) {				//若编辑框中的字符不为0时执行下面的语句
					string=string+"0";					//在字符串后面添加0
					jTextField.setText(string);			//将新的字符串设置到文本框里
				}
			}
		});
		jFrame.add(jButton0);
		
		JButton jButton1=new JButton("1");								//添加按钮1
		jButton1.setBounds(15, 300, 50, 50);
		jButton1.setHorizontalAlignment(JButton.CENTER);
		jButton1.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton1.addActionListener(new ActionListener() {				//为按钮添加事件监听器
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("1");
				} else {
					string=string+"1";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton1);
		
		JButton jButton2=new JButton("2");
		jButton2.setBounds(85, 300, 50, 50);
		jButton2.setHorizontalAlignment(JButton.CENTER);
		jButton2.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("2");
				} else {
					string=string+"2";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton2);
		
		JButton jButton3=new JButton("3");
		jButton3.setBounds(155, 300, 50, 50);
		jButton3.setHorizontalAlignment(JButton.CENTER);
		jButton3.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("3");
				} else {
					string=string+"3";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton3);
		
		JButton jButton4=new JButton("4");
		jButton4.setBounds(15, 230, 50, 50);
		jButton4.setHorizontalAlignment(JButton.CENTER);
		jButton4.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("4");
				} else {
					string=string+"4";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton4);
		
		JButton jButton5=new JButton("5");
		jButton5.setBounds(85, 230, 50, 50);
		jButton5.setHorizontalAlignment(JButton.CENTER);
		jButton5.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("5");
				} else {
					string=string+"5";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton5);
		
		JButton jButton6=new JButton("6");
		jButton6.setBounds(155, 230, 50, 50);
		jButton6.setHorizontalAlignment(JButton.CENTER);
		jButton6.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton6.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("6");
				} else {
					string=string+"6";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton6);
		
		JButton jButton7=new JButton("7");
		jButton7.setBounds(15, 160, 50, 50);
		jButton7.setHorizontalAlignment(JButton.CENTER);
		jButton7.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton7.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("7");
				} else {
					string=string+"7";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton7);
		
		JButton jButton8=new JButton("8");
		jButton8.setBounds(85, 160, 50, 50);
		jButton8.setHorizontalAlignment(JButton.CENTER);
		jButton8.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton8.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("8");
				} else {
					string=string+"8";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton8);
		
		JButton jButton9=new JButton("9");
		jButton9.setBounds(155, 160, 50, 50);
		jButton9.setHorizontalAlignment(JButton.CENTER);
		jButton9.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButton9.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (string.equals("0")) {
					jTextField.setText("9");
				} else {
					string=string+"9";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButton9);
		
		JButton jButtonDot=new JButton(".");
		jButtonDot.setBounds(155, 370, 50, 50);
		jButtonDot.setHorizontalAlignment(JButton.CENTER);
		jButtonDot.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonDot.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String string=jTextField.getText().toString().trim();
				if (!string.contains(".")) {
					string=string+".";
					jTextField.setText(string);
				}
			}
		});
		jFrame.add(jButtonDot);
		
		JButton jButtonClr=new JButton("C");								//添加按钮C
		jButtonClr.setBounds(15, 90, 120, 50);
		jButtonClr.setHorizontalAlignment(JButton.CENTER);
		jButtonClr.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonClr.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonClr);
		//实现计算
		JButton jButtonPls=new JButton("+");
		jButtonPls.setBounds(225, 230, 50, 50);
		jButtonPls.setHorizontalAlignment(JButton.CENTER);
		jButtonPls.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonPls.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					x=Double.parseDouble(jTextField.getText().toString().trim());//出现异常的语句
				} catch (Exception e2) {				//异常信息
					String string=e2.getMessage();		//捕获异常之后如何处理
					jTextField.setText(string);			
				}
				op="+";
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonPls);
		
		JButton jButtonMns=new JButton("-");
		jButtonMns.setBounds(225, 160, 50, 50);
		jButtonMns.setHorizontalAlignment(JButton.CENTER);
		jButtonMns.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonMns.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					x=Double.parseDouble(jTextField.getText().toString().trim());//出现异常的语句
				} catch (Exception e2) {				//异常信息
					String string=e2.getMessage();		//捕获异常之后如何处理
					jTextField.setText(string);			
				}
				op="-";
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonMns);
		
		JButton jButtonMtp=new JButton("×");
		jButtonMtp.setBounds(225, 90, 50, 50);
		jButtonMtp.setHorizontalAlignment(JButton.CENTER);
		jButtonMtp.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonMtp.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					x=Double.parseDouble(jTextField.getText().toString().trim());//出现异常的语句
				} catch (Exception e2) {				//异常信息
					String string=e2.getMessage();		//捕获异常之后如何处理
					jTextField.setText(string);			
				}
				op="*";
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonMtp);
		
		JButton jButtonDiv=new JButton("÷");
		jButtonDiv.setBounds(155, 90, 50, 50);
		jButtonDiv.setHorizontalAlignment(JButton.CENTER);
		jButtonDiv.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonDiv.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					x=Double.parseDouble(jTextField.getText().toString().trim());//出现异常的语句
				} catch (Exception e2) {				//异常信息
					String string=e2.getMessage();		//捕获异常之后如何处理
					jTextField.setText(string);			
				}
				op="/";
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonDiv);
		
		JButton jButtonEql=new JButton("=");
		jButtonEql.setBounds(225, 300, 50, 120);
		jButtonEql.setHorizontalAlignment(JButton.CENTER);
		jButtonEql.setFont(new Font("Product Sans", Font.PLAIN, 24));
		jButtonEql.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				double y=Double.parseDouble(jTextField.getText().toString().trim());
				if (op.equals("+")) {
					double z=x+y;
					jTextField.setText(Double.toString(z));
				}
				if (op.equals("-")) {
					double z=x-y;
					jTextField.setText(Double.toString(z));
				}
				if (op.equals("*")) {
					double z=x*y;
					jTextField.setText(Double.toString(z));
				}
				if (op.equals("/")) {
					if (y==0) {
						jTextField.setText("不能除以0");
					} else {
						double z=x/y;
						jTextField.setText(Double.toString(z));
					}
				}
			}
		});
		jFrame.add(jButtonEql);
		//实现时分秒计时器
		JTextField jTextFieldSec=new JTextField("0");						//添加秒文本框
		jTextFieldSec.setBounds(195, 460, 50, 50);
		jTextFieldSec.setHorizontalAlignment(JTextField.CENTER);			//设置文本居中对齐
		jTextFieldSec.setFont(new Font("Product Sans", Font.BOLD, 30));			//设置字体、字型、字号
		jFrame.add(jTextFieldSec);
		
		JTextField jTextFieldMin=new JTextField("0");						//添加文本框
		jTextFieldMin.setBounds(105, 460, 50, 50);
		jTextFieldMin.setHorizontalAlignment(JTextField.CENTER);			//设置文本剧中对齐
		jTextFieldMin.setFont(new Font("Product Sans", Font.BOLD, 30));			//设置字体、字型、字号
		jFrame.add(jTextFieldMin);
		
		JTextField jTextFieldHrs=new JTextField("0");						//添加文本框
		jTextFieldHrs.setBounds(15, 460, 50, 50);
		jTextFieldHrs.setHorizontalAlignment(JTextField.CENTER);			//设置文本居中对齐
		jTextFieldHrs.setFont(new Font("Product Sans", Font.BOLD, 30));			//设置字体、字型、字号
		jFrame.add(jTextFieldHrs);
		
		Thread thread=new Thread(new Runnable() {
			@Override
			public void run() {					//当线程被启动后自动执行此处代码
				int s=0,m=0,h=0;
				while (true) {					//无限循环语句
					try {
						Thread.sleep(1000);		//使线程等待1000ms
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					jTextFieldSec.setText(""+s++);
				}
			}
		});
		thread.start();
		
		jFrame.setVisible(true);
	}
	
}

